package com.codeest.geeknews.ui.main.activity;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.codeest.geeknews.R;
import com.codeest.geeknews.base.RootActivity;
import com.codeest.geeknews.component.OkhttpDownloadService;
import com.codeest.geeknews.ui.main.adapter.DownloadAdapter;
import com.codeest.geeknews.util.ToastUtil;
import com.codeest.geeknews.util.okhttp.DownloadInfo;
import com.codeest.geeknews.util.okhttp.OkhttpDownloadServiceUtils;
import com.codeest.geeknews.widget.decoration.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by admin on 2017/7/19.
 */

public class DownloadPageActivity extends RootActivity implements ServiceConnection {

    private final static String TAG = "DownloadPageActivity";

    @BindView(R.id.toolbar)
    Toolbar toolBar;

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    private DownloadAdapter mAdapter;
    private OkhttpDownloadServiceUtils downloadServiceUtils;
    private OkhttpDownloadServiceUtils.ServiceToken mToken;
    private List<DownloadInfo> downloadInfoList;
    private String downloadUrl1 = "http://ucdl.25pp.com/fs08/2017/06/12/2/109_31a0241afcf4463bbc580a1f403358fd.apk";
    private String downloadUrl2 = "http://static.ruanko.com/course/2016-03-22/8a22b4205354de5e01537882242300ee/video_document/c82bc0c5bf5f42e1bcb0354a086e6e80.doc";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mToken = downloadServiceUtils.bindToService(this, this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        downloadServiceUtils.addOnProgressListener(progressListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        downloadServiceUtils.removeOnProgressListener(progressListener);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        downloadServiceUtils.unbindFromService(mToken);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.download_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_download_add) {
            downloadServiceUtils.startDownload(downloadUrl1);
            downloadServiceUtils.startDownload(downloadUrl2);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_download;
    }

    @Override
    protected void initEventAndData() {
        setToolBar(toolBar, "下载列表");
        downloadServiceUtils = OkhttpDownloadServiceUtils.getInstance();
        downloadInfoList = new ArrayList<>();
        mAdapter = new DownloadAdapter(this, downloadInfoList);
        //CommonItemDecoration mDecoration = new CommonItemDecoration(2, CommonItemDecoration.UNIT_PX);
        SpacesItemDecoration mDecoration = new SpacesItemDecoration(0, 1, Color.GRAY);
        recyclerview.addItemDecoration(mDecoration);
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerview.setAdapter(mAdapter);
    }

    @Override
    protected void initInject() {

    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        List<DownloadInfo> downloadInfoList = downloadServiceUtils.getDownloads();
        mAdapter.update(downloadInfoList);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        //downloadService.removeOnProgressListener(progressListener);
    }

    OkhttpDownloadService.OnProgressListener progressListener = new OkhttpDownloadService.OnProgressListener() {
        @Override
        public void onProgress(float fraction, DownloadInfo downloadInfo) {
            Log.i(TAG, "下载进度：" + fraction);
            mAdapter.update(downloadInfo);
//            List<DownloadInfo> downloadInfoList = downloadServiceUtils.getDownloads();
//            mAdapter.update(downloadInfoList);
            if (fraction >= 1.0 && mToken != null) {
                downloadServiceUtils.unbindFromService(mToken);
                ToastUtil.shortShow("下载完成！");
                mAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onComplete(DownloadInfo downloadInfo) {

        }
    };
}
